$include("$theme/feedPillow.css");

with ($ns("fp.renderers"))
{
    fp.renderers.FeedPillowRenderer = function()
    {
        var me = this;
        
        var _$content = $("<div/>");
        
        me.renderFeed = function(p_feed, $feed)
        {
            var $feed = null;
            
            
            try
            {
                var html = fp.utils.ContentExtractor.extractContent(p_feed);
                var thumbnail = fp.utils.ContentExtractor.extractThumbnail(html);
                if (thumbnail == null)
                {
                    thumbnail = {
                        url: $mappath("~/images/error_404.png"),
                        width: 192,
                        height: 154
                    };
                }
                $feed = _renderFeed(p_feed, thumbnail);
            }
            catch (e)
            {
                console.error(e.toString());
            }
            
            return $feed;
        };
        
        
        
        function _renderFeed(p_feed, p_thumbnail)
        {            
            var $feed = $("<div class='feedPillow video'>");            
            $feed.data("feed", p_feed);
            $feed.attr("id", p_feed.id);
            
            var $frame = $("<div id='frame'/>");
            $feed.append($frame);
            
            var $imgLink = $("<a id='thumbnail' target='_blank'>");
            if (p_feed.alternate != null && p_feed.alternate.length > 0)
            {
                $imgLink.attr("href", p_feed.alternate[0].href);
            }
            var $img = $("<img />");
            $img.error(_img_error);
            $img.attr("src", p_thumbnail.url);
            
            
            if (p_feed.content != null && p_feed.content.content.indexOf(".swf") != -1 || p_feed.summary != null && p_feed.summary.content.indexOf(".swf") != -1)
            {
                var $video = $("<div id='video' />");
                $imgLink.append($video);
            }
            
            var rate = 192 / p_thumbnail.width;
            $img.attr("width", 192);
            if (p_thumbnail.height > 0)
            {
                $img.attr("height", p_thumbnail.height * rate);
            }
            
            $imgLink.append($img);
            $frame.append($imgLink);
            
            
            var $title = $("<div id='title' />");
            $title.html(p_feed.title ? p_feed.title : ".");
            $frame.append($title);
            
            /*
            var $author = $("<div id='author' />");
            $author.text(p_feed.author ? p_feed.author : ".");
            $info.append($author);
            */
            

            
            var $info = $("<div id='info'>");
            
            var $star = $("<span id='star'>");
            $star.attr("title", $msg("fp", "add_star"));
            $star.click(_star_click);
            var starred = false;
            if (typeof(p_feed.starred) != "undefined")
            {
                starred = p_feed.starred;
            }
            else
            {
                for (var i = 0; i < p_feed.categories.length; i++)
                {
                    if (p_feed.categories[i].endsWith("/state/com.google/starred"))
                    {
                        p_feed.starred = true;
                        starred = true;
                        break;
                    }
                }
            }
            if (starred)
            {
                $feed.addClass("starred");
                $star.attr("title", $msg("fp", "remove_star"));
            }
            $info.append($star);
            
            var $originLink = $("<a id='origin' target='_blank' />");
            $originLink.text(p_feed.origin.title ? p_feed.origin.title : ".");
            $originLink.attr("title", $originLink.text());
            $originLink.attr("href", p_feed.origin.htmlUrl);
            $originLink.click(_originLink_click);
            $info.append($originLink);
            
            var $publishTime = $("<div id='publishTime' />");
            $publishTime.text(fp.utils.DateTimeUtil.getShortDateTimeString(p_feed.crawlTimeMsec));
            $info.append($publishTime);
            
            $frame.append($info);
            
            $feed.append("<div id='shadow'>");
            return $feed;
        }
        
        
        function _img_error(e)
        {
            $(this).attr({
                src: $mappath("~/images/error_404.png"),
                width: 192,
                height: 154
            });
        }
        
        function _star_click(e)
        {
            var $img = $(this);
            var $e = $img.closest(".feedPillow");
            var feed = $e.data("feed");
            if ($e.hasClass("starred"))
            {
                feed.starred = false;
                g.Reader.removeStar(feed);
                $e.removeClass("starred");
                $img.attr("title", $msg("fp", "add_star"));
            }
            else
            {
                feed.starred = true;
                g.Reader.addStar(feed);
                $e.addClass("starred");
                $img.attr("title", $msg("fp", "remove_star"));
            }
        }
        
        
        
        
        function _originLink_click(e)
        {
            if (e.button != 0 || e.shiftKey || e.ctrlKey || !e.altKey)
            {
                return;
            }
            
            e.preventDefault();
            
            var $link = $(this);
            var $e = $link.closest(".feedPillow");
            var feed = $e.data("feed");
            if (feed != null)
            {
                fp.AppMain.load(feed.origin.streamId);
            }
        }
        
        return me;
    };
}